﻿//****************************************************************************
//
//  File:       RibbonCOMInterfaces.cs
//
//  Contents:   Interfaces of the Windows Ribbon Framework, based on 
//              UIRibbon.idl from windows 7 SDK
//
//****************************************************************************

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
using MS.WindowsAPICodePack.Internal;

namespace RibbonLib.Interop
{
    // Windows Ribbon interfaces implemented by the framework

    [Serializable]
    internal enum UI_ContextAvailability
    {
        NotAvailable = 0,
        Available = 1,
        Active = 2,
    }


    [Serializable]
    internal enum UI_FontProperties
    {
        NotAvailable = 0,
        NotSet = 1,
        Set = 2,
    }


    [Serializable]
    internal enum UI_FontVerticalPosition
    {
        NotAvailable = 0,
        NotSet = 1,
        SuperScript = 2,
        SubScript = 3,
    }


    [Serializable]
    internal enum UI_FontUnderline
    {
        NotAvailable = 0,
        NotSet = 1,
        Set = 2,
    }


    [Serializable]
    internal enum UI_FontDeltaSize
    {
        Grow = 0,
        Shrink = 1,
    }


    [Serializable]
    internal enum UI_ControlDock
    {
        Top = 1,
        Bottom = 3,
    }


    // Types for the color picker

    // Determines whether a swatch has a color, is nocolor or automatic.
    [Serializable]
    internal enum UI_SwatchColorType
    {
        NoColor = 0,    // Inactive swatch
        Automatic = 1,  // Automatic swatch
        RGB = 2,        // Solid color swatch
    }


    // If the mode is set to MONOCHROME, the swatch's RGB color value will be interpreted as a 1 bit-per-pixel
    // pattern.
    [Serializable]
    internal enum UI_SwatchColorMode
    {
        Normal = 0,
        Monochrome = 1,
    }


    // Simple property bag
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid(RibbonIIDGuid.IUISimplePropertySet)]
    public interface IUISimplePropertySet
    {
        // Retrieves the stored value of a given property
        [PreserveSig]
        HRESULT GetValue([In] ref PropertyKey key, [Out()] out PropVariant value);
    }


    // Ribbon view interface
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid(RibbonIIDGuid.IUIRibbon)]
    internal interface IUIRibbon
    {
        // Returns the Ribbon height
        [PreserveSig]
        HRESULT GetHeight(out UInt32 cy);

        // Load Ribbon parameters (e.g. QuickAccessToolbar) from a stream
        [PreserveSig]
        HRESULT LoadSettingsFromStream([MarshalAs(UnmanagedType.Interface)] IStream pStream);

        // Save Ribbon parameters (e.g. QuickAccessToolbar) to a stream
        [PreserveSig]
        HRESULT SaveSettingsToStream([MarshalAs(UnmanagedType.Interface)] IStream pStream);
    }


    [Serializable, Flags]
    public enum UI_Invalidations
    {
        State = 0X00000001,             // UI_PKEY_Enabled
        Value = 0X00000002,             // Value property
        Property = 0X00000004,          // Any property
        AllProperties = 0X00000008,     // All properties
    }


    // const UINT32 UI_All_Commands = 0;


    // Windows Ribbon Application interface
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid(RibbonIIDGuid.IUIFramework)]
    public interface IUIFramework
    {
        // Connects the framework and the application
        [PreserveSig]
        HRESULT Initialize(IntPtr frameWnd, IUIApplication application);

        // Releases all framework objects
        [PreserveSig]
        HRESULT Destroy();

        // Loads and instantiates the views and commands specified in markup
        [PreserveSig]
        HRESULT LoadUI(IntPtr instance, [MarshalAs(UnmanagedType.LPWStr)] string resourceName);

        // Retrieves a pointer to a view object
        [PreserveSig]
        HRESULT GetView(UInt32 viewId, Guid riid, [Out(), MarshalAs(UnmanagedType.Interface)] out object /* [out, iid_is(riid), annotation("__deref_out")] void** */ ppv);

        // Retrieves the current value of a property
        [PreserveSig]
        HRESULT GetUICommandProperty(UInt32 commandId, [In] ref PropertyKey key, out PropVariant value);

        // Immediately sets the value of a property
        [PreserveSig]
        HRESULT SetUICommandProperty(UInt32 commandId, [In] ref PropertyKey key, [In] ref PropVariant value);

        // Asks the framework to retrieve the new value of a property at the next update cycle
        [PreserveSig]
        HRESULT InvalidateUICommand(UInt32 commandId, UI_Invalidations flags, [In] ref PropertyKey key);

        // Flush all the pending UI command updates
        [PreserveSig]
        HRESULT FlushPendingInvalidations();

        // Asks the framework to switch to the list of modes specified and adjust visibility of controls accordingly
        [PreserveSig]
        HRESULT SetModes(Int32 iModes);
    }


    // Windows Ribbon ContextualUI interface
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid(RibbonIIDGuid.IUIContextualUI)]
    internal interface IUIContextualUI
    {
        // Sets the desired anchor point where ContextualUI should be displayed.
        // Typically this is the mouse location at the time of right click.
        // x and y are in virtual screen coordinates.
        [PreserveSig]
        HRESULT ShowAtLocation(Int32 x, Int32 y);
    }


    // Windows Ribbon Collection interface
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid(RibbonIIDGuid.IUICollection)]
    internal interface IUICollection
    {
        // Retrieves the count of the collection
        [PreserveSig]
        HRESULT GetCount(out UInt32 count);

        // Retrieves an item
        [PreserveSig]
        HRESULT GetItem(UInt32 index, [Out(), MarshalAs(UnmanagedType.Interface)] out object /* IUnknown** */ item);

        // Adds an item to the end
        [PreserveSig]
        HRESULT Add([MarshalAs(UnmanagedType.Interface)] object item);

        // Inserts an item
        [PreserveSig]
        HRESULT Insert(UInt32 index, [MarshalAs(UnmanagedType.Interface)] object item);

        // Removes an item at the specified position
        [PreserveSig]
        HRESULT RemoveAt(UInt32 index);

        // Replaces an item at the specified position
        [PreserveSig]
        HRESULT Replace(UInt32 indexReplaced, [MarshalAs(UnmanagedType.Interface)] object itemReplaceWith);

        // Clear the collection
        [PreserveSig]
        HRESULT Clear();
    }


    [Serializable]
    internal enum UI_CollectionChange
    {
        Insert = 0,
        Remove = 1,
        Replace = 2,
        Reset = 3,
    }


    // const UINT32 UI_Collection_InvalidIndex = 0xffffffff;


    // Connection Sink for listening to collection changes
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid(RibbonIIDGuid.IUICollectionChangedEvent)]
    internal interface IUICollectionChangedEvent
    {
        [PreserveSig]
        HRESULT OnChanged(UI_CollectionChange action,
                          UInt32 oldIndex, [MarshalAs(UnmanagedType.Interface)] object oldItem,
                          UInt32 newIndex, [MarshalAs(UnmanagedType.Interface)] object newItem);
    }


    // Windows Ribbon interfaces implemented by the application


    [Serializable]
    public enum UI_ExecutionVerb
    {
        Execute = 0,
        Preview = 1,
        CancelPreview = 2,
    }


    // Command handler interface
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid(RibbonIIDGuid.IUICommandHandler)]
    public interface IUICommandHandler
    {
        // User action callback, with transient execution parameters
        [PreserveSig]
        HRESULT Execute(UInt32 commandId,                      // the command that has been executed
                        UI_ExecutionVerb verb,                 // the mode of execution
                        [In] ref PropertyKey key,              // the property that has changed
                        [In] ref PropVariant currentValue,     // the new value of the property that has changed
                        [MarshalAs(UnmanagedType.Interface)] IUISimplePropertySet commandExecutionProperties); // additional data for this execution

        // Informs of the current value of a property, and queries for the new one
        [PreserveSig]
        HRESULT UpdateProperty(UInt32 commandId,
                               [In] ref PropertyKey key,
                               [In] ref PropVariant currentValue,
                               [In, Out] ref PropVariant newValue);
    }


    // Types of UI commands
    [Serializable]
    public enum UI_CommandType
    {
        Unknown = 0,
        Group = 1,
        Action = 2,
        Anchor = 3,
        Context = 4,
        Collection = 5,
        Commandcollection = 6,
        Decimal = 7,
        Boolean = 8,
        Font = 9,
        RecentItems = 10,
        ColorAnchor = 11,
        ColorCollection = 12,
    }


    // Types of UI Views
    [Serializable]
    public enum UI_ViewType
    {
        Ribbon = 1,
    }


    [Serializable]
    public enum UI_ViewVerb
    {
        Create = 0,
        Destroy = 1,
        Size = 2,
        Error = 3,
    }


    // Application callback interface
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid(RibbonIIDGuid.IUIApplication)]
    public interface IUIApplication
    {
        // A view has changed
        [PreserveSig]
        HRESULT OnViewChanged(UInt32 viewId,
                              UI_ViewType typeID,
                              [MarshalAs(UnmanagedType.Interface)] object view,
                              UI_ViewVerb verb,
                              int uReasonCode);

        // Command creation callback
        [PreserveSig]
        HRESULT OnCreateUICommand(UInt32 commandId,
                                  UI_CommandType typeID,
                                  [Out, MarshalAs(UnmanagedType.Interface)] out IUICommandHandler commandHandler);

        // Command destroy callback
        [PreserveSig]
        HRESULT OnDestroyUICommand(UInt32 commandId,
                                   UI_CommandType typeID,
                                   [MarshalAs(UnmanagedType.Interface)] IUICommandHandler commandHandler);
    }


    // Container for bitmap image
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid(RibbonIIDGuid.IUIImage)]
    internal interface IUIImage
    {
        [PreserveSig]
        HRESULT GetBitmap(out IntPtr /* HBITMAP* */ bitmap);
    }


    [Serializable]
    internal enum UI_Ownership
    {
        Transfer = 0, // IUIImage now owns HBITMAP.
        Copy = 1, // IUIImage creates a copy of HBITMAP. Caller still owns HBITMAP.
    }

    // Produces containers for bitmap images
    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid(RibbonIIDGuid.IUIImageFromBitmap)]
    internal interface IUIImageFromBitmap
    {
        [PreserveSig]
        HRESULT CreateImage(IntPtr /* HBITMAP */ bitmap, UI_Ownership options, [Out(), MarshalAs(UnmanagedType.Interface)] out IUIImage image);
    }

}
